package net.uhuijia.goods.goodspu.service.impl;

import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.enums.GoodIntroduceType;
import net.uhuijia.exception.BusinessException;
import net.uhuijia.goods.goodbanner.service.GoodBannerService;
import net.uhuijia.goods.goodintroduce.service.GoodIntroduceService;
import net.uhuijia.goods.goodskuproperty.service.GoodSkuPropertyService;
import net.uhuijia.shop.info.clientservice.ShopInfoClientService;
import net.uhuijia.user.usermall.clientservice.UserMallClientService;
import net.uhuijia.vo.good.*;
import net.uhuijia.vo.shop.ShopBaseDetaiVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.goods.goodspu.persistence.dao.GoodSpuDao;
import net.uhuijia.goods.goodspu.persistence.model.GoodSpu;
import net.uhuijia.goods.goodspu.persistence.model.GoodSpuExample;
import net.uhuijia.goods.goodspu.service.GoodSpuService;

import java.util.List;


@Service
@Transactional
public class GoodSpuServiceImpl
     extends CrudService<GoodSpuDao,GoodSpu,GoodSpuExample>
     implements  GoodSpuService {

    @Autowired
    private GoodSkuPropertyService goodSkuPropertyService;
    @Autowired
    private GoodBannerService goodBannerService;
    @Autowired
    private GoodIntroduceService goodIntroduceService;
    @Autowired
    private ShopInfoClientService shopInfoClientService;
    @Autowired
    private UserMallClientService userMallClientService;
    /**
     * 将GoodSpu 转换成 GoodZBListVo
     * @param spuId
     * @return
     */
    @Override
    public GoodZBListVo goodSpuWrapToGoodZBListVo(String spuId) {
        GoodSpu goodSpu =  this.get(spuId);
        GoodZBListVo goodZBListVo = new GoodZBListVo();
        goodZBListVo.setMainImage(goodSpu.getMainSkuImage());
        goodZBListVo.setPrice(goodSpu.getMainSkuPrice());
        goodZBListVo.setSpuId(goodSpu.getId());
        return goodZBListVo;
    }
    /**
     * @Author xzj
     * @Description //商品属性规格弹窗数据
     * @Date  2018/10/6
     * @Param
     * @return
     **/
    @Override
    public GoodSkuDetailVo getGoodSkuDetail(String spuId) {
        if(StringUtils.isEmpty(spuId))
            throw new IllegalArgumentException();
        GoodSpu goodSpu = this.get(spuId);
        if(goodSpu!=null){
            GoodSkuDetailVo goodSkuDetailVo = GoodSkuDetailVo.generalGoodSkuDetailVo(goodSpu.getId(),goodSpu.getMainSkuImage(),goodSpu.getTotalSales(),goodSpu.getTotalInvertory(),goodSpu.getGoodName());
            List<GoodSkuVo> goodSkuVos =this.goodSkuPropertyService.getGoodSkuVoBySpuId(spuId);
            goodSkuDetailVo.setGoodSkuVos(goodSkuVos);
            return goodSkuDetailVo;
        }else{
            throw new BusinessException(ErrorCodeEnum.good_already_down_error);
        }
    }

    @Override
    public GoodDetailVo getGoodDetail(String spuId) {
        if(StringUtils.isEmpty(spuId))
            throw new IllegalArgumentException();
        GoodDetailVo goodDetailVo = new GoodDetailVo();
        GoodSpu goodSpu = this.get(spuId);
        if(goodSpu!=null){
            /**商品基本信息*/
            goodDetailVo.setSpuId(spuId);
            goodDetailVo.setGoodName(goodSpu.getGoodName());
            goodDetailVo.setPrice(goodSpu.getMainSkuPrice());
            goodDetailVo.setFee(goodSpu.getFreight()); //运费
            /**商品轮播图*/
            List<GoodBannerVo> goodBannerVos = this.goodBannerService.findGoodBannerVoBySpuId(goodSpu.getId());
            goodDetailVo.setBanners(goodBannerVos);
            //TODO /**店铺信息*/
            String shopId = goodSpu.getShopId(); //店铺Id
            ShopBaseDetaiVo shopBaseDetaiVo = this.shopInfoClientService.getShopBaseDetaiVo(shopId);
            if(shopBaseDetaiVo!=null){
                goodDetailVo.setShopBaseDetaiVo(shopBaseDetaiVo);
            }
            /** 图文详情*/
            List<String> detailImages = this.goodIntroduceService.findGoodIntroduceImagesBySpuIdAndType(goodSpu.getId(), GoodIntroduceType.detail);
            goodDetailVo.setDetailImages(detailImages);
            /**商品参数*/
            List<String> goodSkuImage = this.goodIntroduceService.findGoodIntroduceImagesBySpuIdAndType(goodSpu.getId(),GoodIntroduceType.goodSku);
            goodDetailVo.setGoodSkuImages(goodSkuImage);
            return goodDetailVo;
        }else{
            throw new BusinessException(ErrorCodeEnum.good_already_down_error);
        }
    }
}